package com.reger.core.exception;

/**
 * 业务异常类.
 * 
 * @author Administrator
 *
 */
public class GlobalException extends RuntimeException {

	private static final long serialVersionUID = 1L;
	private int code;
	private String describe;
	private String details;
	private Level level;

	private void init(ExceptionStatus exceptionstatus) {
		this.code = exceptionstatus.getCode();
		this.describe = exceptionstatus.getMessage();
		this.level = exceptionstatus.getLevel();
	}

	@Override
	public String getMessage() {
		String msg = super.getMessage();
		StringBuffer message = new StringBuffer();
		if (this.level != null)
			message.append("  异常级别:").append(this.level);
		message.append("  异常代码:").append(this.code);
		if (this.describe != null)
			message.append("  英文描述:").append(this.describe);
		if (this.details != null)
			message.append("  详细描述:").append(this.details);
		if (msg != null)
			message.append("  原始描述:").append(msg);
		return message.toString();
	}

	@Override
	public String toString() {
		return this.getMessage();
	}

	public GlobalException(final ExceptionStatus exceptionstatus) {
		this.init(exceptionstatus);
	}

	public GlobalException(final ExceptionStatus exceptionstatus, final Throwable throwable) {
		super(throwable);
		this.init(exceptionstatus);
	}

	public GlobalException(final ExceptionStatus exceptionstatus, final Throwable throwable, final String details) {
		this(exceptionstatus, throwable);
		this.details = details;
	}

	public GlobalException(final ExceptionStatus exceptionstatus, final String details) {
		this(exceptionstatus);
		this.details = details;
	}

	public final static void throwException(final ExceptionStatus exceptionstatus) {
		throw new GlobalException(exceptionstatus);
	}
	
	public final static void throwException(final ExceptionStatus exceptionstatus, final Throwable throwable) {
		throw new GlobalException(exceptionstatus, throwable);
	}

	public final static void throwException(final ExceptionStatus exceptionstatus, final Throwable throwable,
			final String details) {
		throw new GlobalException(exceptionstatus, throwable, details);
	}

	public final static void throwException(final ExceptionStatus exceptionstatus, final String details) {
		throw new GlobalException(exceptionstatus, details);
	}

	public final static void error( final String details) {
		throw new GlobalException(GlobalExceptionStatus.ERROR, details);
	}
	public final static void warn( final String details) {
		throw new GlobalException(GlobalExceptionStatus.WARN, details);
	}
	public final static void info( final String details) {
		throw new GlobalException(GlobalExceptionStatus.INFO, details);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public Level getLevel() {
		return level;
	}

	public void setLevel(Level level) {
		this.level = level;
	}
}
